<template>
  <pf-main :title="'创建表单'">
    <div class="pf-mt-20">
      <pf-form ref="formFDom" :config="formFConfig" :model="formInfo"></pf-form>
    </div>
    <div class="pf-text-right pf-mr-20">
      <el-button icon="Refresh" v-if="isSave" @click="refresh"></el-button>
      <el-button type="primary" v-if="!isSave" @click="saveForm">保存</el-button>
      <el-button type="primary" v-if="isSave" @click="create">创建</el-button>
      <el-button
        type="primary"
        v-if="isSave"
        @click="
          () => {
            importDbShow = true;
          }
        "
        >导入数据表</el-button
      >
      <el-button v-if="isSave" @click="del">删除</el-button>
      <el-button type="primary" v-if="isSave" @click="preview">预览</el-button>
    </div>
    <pf-table class="pf-mt-20" :config="fieldTConfig" :data="fieldList">
      <el-table-column label="操作" fixed="right" width="80">
        <template #default="scope">
          <el-tooltip effect="light" placement="left" trigger="hover">
            <component :is="'Menu'" class="el-button--text" style="width: 18px"></component>
            <template #content>
              <div class="pf-flex pf-flex-column pf-flex-0">
                <el-button @click="handleClick(scope.row, 'edit')" link type="primary" size="mini"
                  >编辑</el-button
                >
                <el-button @click="handleClick(scope.row, 'del')" link type="primary" size="mini"
                  >删除</el-button
                >
              </div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
    </pf-table>
    <el-dialog :width="'80%'" title="表单项" v-model="show">
      <pf-form ref="fieldFDom" :config="fieldFConfig" :model="fieldInfo" :col-count="3"></pf-form>
      <template #footer>
        <div class="pf-text-right">
          <el-button type="primary" @click="confirmCreate">确认</el-button>
          <el-button @click="show = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :width="'80%'" title="表单预览" v-model="previewShow">
      <pf-form :config="previewFormConfig" :model="previewInfo" :col-count="3"></pf-form>
      <template #footer>
        <div class="pf-text-right">
          <el-button type="primary" @click="previewOver">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <import-for-db
      :show="importDbShow"
      :form-id="formInfo.formId"
      @close="closeImportDbDialog"
    ></import-for-db>
  </pf-main>
</template>

<script lang="ts">
import { defineComponent, onMounted, Ref, ref } from 'vue';
import { useNotice } from '@/components/element-plus/notice';
import { systemApi } from '@/constants/api/system-api';
import {
  emptyForm,
  emptyTable,
  FormModel,
  ResponseCodeEnum,
  SysFormField,
  SysFormInfo,
  TableModel,
  useData,
  useHttpClient
} from 'pf-component';
import { FormNameEnum } from '@/constants/enum/form-name.enum';
import ImportForDb from '@/views/page-config/dialog/ImportForDb.vue';
import { TableNameEnum } from '@/constants/enum/table-name.enum';
import { copy } from '@/constants/util/objects-utils';

export default defineComponent({
  name: 'CreateFormConfig',
  components: { ImportForDb },
  props: {
    name: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const { general } = useHttpClient();
    const { loadDictField } = useData();
    const { message, loading } = useNotice();

    const isSave: Ref<boolean> = ref(false);
    const formFDom = ref(null);
    const formFConfig: Ref<FormModel> = ref(emptyForm);
    const formInfo: Ref<SysFormInfo> = ref({} as any);

    const show: Ref<boolean> = ref(false);
    const fieldFDom = ref(null);
    const fieldFConfig: Ref<FormModel> = ref(emptyForm);
    const fieldInfo: Ref<SysFormField> = ref(null as any);

    const previewShow: Ref<boolean> = ref(false);
    const previewFormConfig: Ref<FormModel> = ref({} as FormModel);
    const previewInfo: Ref<any> = ref(null);

    /*列表上方按钮方法*/
    const saveForm = () => {
      (formFDom.value as any).validate((val: boolean) => {
        if (val) {
          general(systemApi.formConfigApi.createForm, undefined, formInfo.value).then(res => {
            if (res.code === ResponseCodeEnum.SUCCESS) {
              message.success(res.message);
              isSave.value = true;
              refresh();
            } else {
              message.error(res.message);
            }
          });
        }
      });
    };
    const create = () => {
      fieldInfo.value = useData().toFormValue(fieldFConfig.value as any);
      show.value = true;
    };
    const confirmCreate = () => {
      const param = copy(fieldInfo.value);
      param.formId = formInfo.value.formId;
      general(systemApi.formConfigApi.updateFormField, undefined, param).then(res => {
        if (res.code === ResponseCodeEnum.SUCCESS) {
          message.success(res.message);
          show.value = false;
          refresh();
        } else {
          message.error(res.message);
        }
      });
    };
    const refresh = () => {
      init(formInfo.value.name);
    };
    const del = () => {
      // systemApi.dictApi.delete
      message.error('待完善');
    };
    const preview = () => {
      formInfo.value.fieldDtos = fieldList.value;
      useData()
        .toFormModel(formInfo.value)
        .then(res => {
          previewFormConfig.value = res;
          previewInfo.value = useData().toFormValue(previewFormConfig.value);
          previewShow.value = true;
        });
    };
    const previewOver = () => {
      previewShow.value = false;
    };

    /*导入数据库表信息*/
    const importDbShow: Ref<boolean> = ref(false);
    const closeImportDbDialog = () => {
      importDbShow.value = false;
    };
    /*表单列，列表信息*/
    const fieldTConfig: Ref<TableModel> = ref(emptyTable);
    const fieldList: Ref<SysFormField[]> = ref([]);
    const handleClick = (data: SysFormField, cmd: string) => {
      switch (cmd) {
        case 'edit':
          // 修改操作 需要将fieldId 赋值
          fieldInfo.value = data;
          show.value = true;
          break;
        case 'del':
          useHttpClient()
            .general(systemApi.formConfigApi.delFormField, { id: data.fieldId })
            .then(res => {
              if (res.code === ResponseCodeEnum.SUCCESS) {
                refresh();
                useNotice().message.success(res.message);
              } else {
                useNotice().message.error(res.message);
              }
            });
          break;
        default:
          message.error('未定义的操作！');
      }
    };

    const init = (name: string) => {
      general<any>(systemApi.formConfigApi.formInfo, { name }).then(res => {
        if (res.code === ResponseCodeEnum.SUCCESS) {
          formInfo.value = res.data.form;
          fieldList.value = res.data.fields;
          isSave.value = true;
          formFConfig.value.setFormDisable();
        } else {
          message.error(res.message);
        }
      });
    };
    onMounted(() => {
      Promise.all([
        useData().loadTable([{ name: TableNameEnum.sysFormField, config: fieldTConfig }]),
        useData().loadForm([
          {
            name: FormNameEnum.sysFieldForm,
            config: fieldFConfig,
            info: fieldInfo
          },
          {
            name: FormNameEnum.sysFormForm,
            config: formFConfig,
            info: formInfo
          }
        ])
      ]).then(ress => {
        if (ress.findIndex(res => !res) === -1) {
          loadDictField().then(res => {
            fieldFConfig.value?.setOptions('dict', res);
            if (props.name) {
              init(props.name);
            }
          });
        }
      });
    });

    return {
      refresh,
      fieldList,
      fieldTConfig,
      handleClick,
      saveForm,
      confirmCreate,
      del,
      create,
      formFDom,
      formFConfig,
      formInfo,
      isSave,
      fieldFDom,
      fieldFConfig,
      fieldInfo,
      importDbShow,
      closeImportDbDialog,
      show,
      preview,
      previewShow,
      previewFormConfig,
      previewInfo,
      previewOver
    };
  }
});
</script>
<style scoped lang="scss">
.#{$prefix} {
  &-create-form-config {
  }
}
</style>
